@extends('Layout/base')
@section('title','登录页面')

@section('main')

    <div class="container">

        <div class="form-group">
            用户名：<input type="text" name="uname" class="form-control">
        </div>

        <div class="form-group">
            密码：<input type="text" name="upass" class="form-control">
        </div>

        <div class="form-group">
            <a href="javascript:;" class="btn btn-danger btn-register">登录</a>
        </div>
    </div>

@endsection

@section('footer-js')
    <script type="text/javascript">
        $('a.btn-register').on('click',function() {

        	var uname = $('input[name="uname"]').val();
        	var upass = $('input[name="upass"]').val();

        	$.ajax({
        		type:'post',
        		url:'{{url("/dologin")}}',
        		data:'name='+uname+'&pass='+upass+'&_token={{csrf_token()}}',
        		success:function(data){

        			if(data.status == 1404){

        				var tip = '<div class="alert alert-danger alter-register-tip">'+data.msg+'</div>';
        			}else if(data.status == 1200) {
        				var tip = '<div class="alert alert-success alter-register-tip">'+data.msg+'</div>';
        				 location.href = '{{url("/")}}';
        			}else if(data.status == 1401) {
        				var tip = '<div class="alert alert-danger alter-register-tip">'+data.msg+'</div>';
        			}

        			setTimeout(function () {
        				$('.alter-register-tip').remove();
        			},3000);

        			$('.container').prepend(tip);

        		},

        		error:function(tipString) {

        			if(tipString.responseText) {
        				var tipDiv = `<div class="alert alert-danger alter-tip">
        <ul>`;


        var tipObj = JSON.parse(tipString.responseText).errors;

        for(var key in tipObj) {

        	tipDiv += '<li>'+tipObj[key][0]+'</li>';
        }

        tipDiv += '</ul></div>';

            $('.alter-tip').remove();
            $('.container').prepend(tipDiv);
        			}
        		},
        		dataType:'json'
        	});
        });
    </script>
@endsection